home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tcl8.0 / compat / strftime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-15  |  9.1 KB  |  386 lines  |  [TEXT/CWIE]

  1. /* 
  2.  * strftime.c --
  3.  *
  4.  *    This file contains a modified version of the BSD 4.4 strftime
  5.  *    function.
  6.  *
  7.  * This file is a modified version of the strftime.c file from the BSD 4.4
  8.  * source.  See the copyright notice below for details on redistribution
  9.  * restrictions.  The "license.terms" file does not apply to this file.
  10.  *
  11.  * SCCS: @(#) strftime.c 1.4 97/08/07 17:17:02
  12.  */
  13.  
  14. /*
  15.  * Copyright (c) 1989 The Regents of the University of California.
  16.  * All rights reserved.
  17.  *
  18.  * Redistribution and use in source and binary forms, with or without
  19.  * modification, are permitted provided that the following conditions
  20.  * are met:
  21.  * 1. Redistributions of source code must retain the above copyright
  22.  *    notice, this list of conditions and the following disclaimer.
  23.  * 2. Redistributions in binary form must reproduce the above copyright
  24.  *    notice, this list of conditions and the following disclaimer in the
  25.  *    documentation and/or other materials provided with the distribution.
  26.  * 3. All advertising materials mentioning features or use of this software
  27.  *    must display the following acknowledgement:
  28.  *    This product includes software developed by the University of
  29.  *    California, Berkeley and its contributors.
  30.  * 4. Neither the name of the University nor the names of its contributors
  31.  *    may be used to endorse or promote products derived from this software
  32.  *    without specific prior written permission.
  33.  *
  34.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  35.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  36.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  37.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  38.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  39.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  40.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  41.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  42.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  43.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  44.  * SUCH DAMAGE.
  45.  */
  46.  
  47. #if defined(LIBC_SCCS) && !defined(lint)
  48. /*static char *sccsid = "from: @(#)strftime.c    5.11 (Berkeley) 2/24/91";*/
  49. static char *rcsid = "$Id: strftime.c,v 1.10.4.1 1996/06/01 22:00:41 jtc Exp $";
  50. #endif /* LIBC_SCCS and not lint */
  51.  
  52. #include <time.h>
  53. #include <string.h>
  54. #include <locale.h>
  55. #include "tclInt.h"
  56. #include "tclPort.h"
  57.  
  58. #define TM_YEAR_BASE   1900
  59.  
  60. typedef struct {
  61.     const char *abday[7];
  62.     const char *day[7];
  63.     const char *abmon[12];
  64.     const char *mon[12];
  65.     const char *am_pm[2];
  66.     const char *d_t_fmt;
  67.     const char *d_fmt;
  68.     const char *t_fmt;
  69.     const char *t_fmt_ampm;
  70. } _TimeLocale;
  71.  
  72. static const _TimeLocale _DefaultTimeLocale = 
  73. {
  74.     {
  75.     "Sun","Mon","Tue","Wed","Thu","Fri","Sat",
  76.     },
  77.     {
  78.     "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
  79.     "Friday", "Saturday"
  80.     },
  81.     {
  82.     "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  83.     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  84.     },
  85.     {
  86.     "January", "February", "March", "April", "May", "June", "July",
  87.     "August", "September", "October", "November", "December"
  88.     },
  89.     {
  90.     "AM", "PM"
  91.     },
  92.     "%a %b %d %H:%M:%S %Y",
  93.     "%m/%d/%y",
  94.     "%H:%M:%S",
  95.     "%I:%M:%S %p"
  96. };
  97.  
  98. static const _TimeLocale *_CurrentTimeLocale = &_DefaultTimeLocale;
  99.  
  100. static size_t gsize;
  101. static char *pt;
  102. static int         _add _ANSI_ARGS_((const char* str));
  103. static int        _conv _ANSI_ARGS_((int n, int digits, int pad));
  104. static int        _secs _ANSI_ARGS_((const struct tm *t));
  105. static size_t        _fmt _ANSI_ARGS_((const char *format,
  106.                 const struct tm *t));
  107.  
  108. size_t
  109. TclStrftime(s, maxsize, format, t)
  110.     char *s;
  111.     size_t maxsize;
  112.     const char *format;
  113.     const struct tm *t;
  114. {
  115.     tzset();
  116.  
  117.     pt = s;
  118.     if ((gsize = maxsize) < 1)
  119.     return(0);
  120.     if (_fmt(format, t)) {
  121.     *pt = '\0';
  122.     return(maxsize - gsize);
  123.     }
  124.     return(0);
  125. }
  126.  
  127. #define SUN_WEEK(t)    (((t)->tm_yday + 7 - \
  128.                 ((t)->tm_wday)) / 7)
  129. #define MON_WEEK(t)    (((t)->tm_yday + 7 - \
  130.                 ((t)->tm_wday ? (t)->tm_wday - 1 : 6)) / 7)
  131.  
  132. static size_t
  133. _fmt(format, t)
  134.     const char *format;
  135.     const struct tm *t;
  136. {
  137.     for (; *format; ++format) {
  138.     if (*format == '%') {
  139.         ++format;
  140.         if (*format == 'E') {
  141.                 /* Alternate Era */
  142.         ++format;
  143.         } else if (*format == 'O') {
  144.                 /* Alternate numeric symbols */
  145.         ++format;
  146.         }
  147.         switch(*format) {
  148.         case '\0':
  149.             --format;
  150.             break;
  151.         case 'A':
  152.             if (t->tm_wday < 0 || t->tm_wday > 6)
  153.             return(0);
  154.             if (!_add(_CurrentTimeLocale->day[t->tm_wday]))
  155.             return(0);
  156.             continue;
  157.         case 'a':
  158.             if (t->tm_wday < 0 || t->tm_wday > 6)
  159.             return(0);
  160.             if (!_add(_CurrentTimeLocale->abday[t->tm_wday]))
  161.             return(0);
  162.             continue;
  163.         case 'B':
  164.             if (t->tm_mon < 0 || t->tm_mon > 11)
  165.             return(0);
  166.             if (!_add(_CurrentTimeLocale->mon[t->tm_mon]))
  167.             return(0);
  168.             continue;
  169.         case 'b':
  170.         case 'h':
  171.             if (t->tm_mon < 0 || t->tm_mon > 11)
  172.             return(0);
  173.             if (!_add(_CurrentTimeLocale->abmon[t->tm_mon]))
  174.             return(0);
  175.             continue;
  176.         case 'C':
  177.             if (!_conv((t->tm_year + TM_YEAR_BASE) / 100,
  178.                 2, '0'))
  179.             return(0);
  180.             continue;
  181.         case 'c':
  182.             if (!_fmt(_CurrentTimeLocale->d_t_fmt, t))
  183.             return(0);
  184.             continue;
  185.         case 'D':
  186.             if (!_fmt("%m/%d/%y", t))
  187.             return(0);
  188.             continue;
  189.         case 'd':
  190.             if (!_conv(t->tm_mday, 2, '0'))
  191.             return(0);
  192.             continue;
  193.         case 'e':
  194.             if (!_conv(t->tm_mday, 2, ' '))
  195.             return(0);
  196.             continue;
  197.         case 'H':
  198.             if (!_conv(t->tm_hour, 2, '0'))
  199.             return(0);
  200.             continue;
  201.         case 'I':
  202.             if (!_conv(t->tm_hour % 12 ?
  203.                 t->tm_hour % 12 : 12, 2, '0'))
  204.             return(0);
  205.             continue;
  206.         case 'j':
  207.             if (!_conv(t->tm_yday + 1, 3, '0'))
  208.             return(0);
  209.             continue;
  210.         case 'k':
  211.             if (!_conv(t->tm_hour, 2, ' '))
  212.             return(0);
  213.             continue;
  214.         case 'l':
  215.             if (!_conv(t->tm_hour % 12 ?
  216.                 t->tm_hour % 12: 12, 2, ' '))
  217.             return(0);
  218.             continue;
  219.         case 'M':
  220.             if (!_conv(t->tm_min, 2, '0'))
  221.             return(0);
  222.             continue;
  223.         case 'm':
  224.             if (!_conv(t->tm_mon + 1, 2, '0'))
  225.             return(0);
  226.             continue;
  227.         case 'n':
  228.             if (!_add("\n"))
  229.             return(0);
  230.             continue;
  231.         case 'p':
  232.             if (!_add(_CurrentTimeLocale->am_pm[t->tm_hour >= 12]))
  233.             return(0);
  234.             continue;
  235.         case 'R':
  236.             if (!_fmt("%H:%M", t))
  237.             return(0);
  238.             continue;
  239.         case 'r':
  240.             if (!_fmt(_CurrentTimeLocale->t_fmt_ampm, t))
  241.             return(0);
  242.             continue;
  243.         case 'S':
  244.             if (!_conv(t->tm_sec, 2, '0'))
  245.             return(0);
  246.             continue;
  247.         case 's':
  248.             if (!_secs(t))
  249.             return(0);
  250.             continue;
  251.         case 'T':
  252.             if (!_fmt("%H:%M:%S", t))
  253.             return(0);
  254.             continue;
  255.         case 't':
  256.             if (!_add("\t"))
  257.             return(0);
  258.             continue;
  259.         case 'U':
  260.             if (!_conv(SUN_WEEK(t), 2, '0'))
  261.             return(0);
  262.             continue;
  263.         case 'u':
  264.             if (!_conv(t->tm_wday ? t->tm_wday : 7, 1, '0'))
  265.             return(0);
  266.             continue;
  267.         case 'V':
  268.         {
  269.                 /* ISO 8601 Week Of Year:
  270.                    If the week (Monday - Sunday) containing
  271.                    January 1 has four or more days in the new 
  272.                    year, then it is week 1; otherwise it is 
  273.                    week 53 of the previous year and the next
  274.                    week is week one. */
  275.                  
  276.             int week = MON_WEEK(t);
  277.  
  278.             int days = (((t)->tm_yday + 7 - \
  279.                 ((t)->tm_wday ? (t)->tm_wday - 1 : 6)) % 7);
  280.  
  281.  
  282.             if (days >= 4) {
  283.             week++;
  284.             } else if (week == 0) {
  285.             week = 53;
  286.             }
  287.  
  288.             if (!_conv(week, 2, '0'))
  289.             return(0);
  290.             continue;
  291.         }
  292.         case 'W':
  293.             if (!_conv(MON_WEEK(t), 2, '0'))
  294.             return(0);
  295.             continue;
  296.         case 'w':
  297.             if (!_conv(t->tm_wday, 1, '0'))
  298.             return(0);
  299.             continue;
  300.         case 'x':
  301.             if (!_fmt(_CurrentTimeLocale->d_fmt, t))
  302.             return(0);
  303.             continue;
  304.         case 'X':
  305.             if (!_fmt(_CurrentTimeLocale->t_fmt, t))
  306.             return(0);
  307.             continue;
  308.         case 'y':
  309.             if (!_conv((t->tm_year + TM_YEAR_BASE) % 100,
  310.                 2, '0'))
  311.             return(0);
  312.             continue;
  313.         case 'Y':
  314.             if (!_conv((t->tm_year + TM_YEAR_BASE), 4, '0'))
  315.             return(0);
  316.             continue;
  317. #ifndef MAC_TCL
  318.         case 'Z': {
  319.             char *name = TclpGetTZName();
  320.             if (name && !_add(name)) {
  321.             return 0;
  322.             }
  323.             continue;
  324.         }
  325. #endif
  326.         case '%':
  327.             /*
  328.              * X311J/88-090 (4.12.3.5): if conversion char is
  329.              * undefined, behavior is undefined.  Print out the
  330.              * character itself as printf(3) does.
  331.              */
  332.         default:
  333.             break;
  334.         }
  335.     }
  336.     if (!gsize--)
  337.         return(0);
  338.     *pt++ = *format;
  339.     }
  340.     return(gsize);
  341. }
  342.  
  343. static int
  344. _secs(t)
  345.     const struct tm *t;
  346. {
  347.     static char buf[15];
  348.     register time_t s;
  349.     register char *p;
  350.     struct tm tmp;
  351.  
  352.     /* Make a copy, mktime(3) modifies the tm struct. */
  353.     tmp = *t;
  354.     s = mktime(&tmp);
  355.     for (p = buf + sizeof(buf) - 2; s > 0 && p > buf; s /= 10)
  356.     *p-- = (char)(s % 10 + '0');
  357.     return(_add(++p));
  358. }
  359.  
  360. static int
  361. _conv(n, digits, pad)
  362.     int n, digits;
  363.     int pad;
  364. {
  365.     static char buf[10];
  366.     register char *p;
  367.  
  368.     for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
  369.     *p-- = (char)(n % 10 + '0');
  370.     while (p > buf && digits-- > 0)
  371.     *p-- = (char) pad;
  372.     return(_add(++p));
  373. }
  374.  
  375. static int
  376. _add(str)
  377.     const char *str;
  378. {
  379.     for (;; ++pt, --gsize) {
  380.     if (!gsize)
  381.         return(0);
  382.     if (!(*pt = *str++))
  383.         return(1);
  384.     }
  385. }
  386.